{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CodeCarbon in a Notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install codecarbon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from codecarbon import EmissionsTracker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[codecarbon INFO @ 15:11:41] [setup] RAM Tracking...\n",
      "[codecarbon INFO @ 15:11:41] [setup] GPU Tracking...\n",
      "[codecarbon INFO @ 15:11:41] No GPU found.\n",
      "[codecarbon INFO @ 15:11:41] [setup] CPU Tracking...\n",
      "[codecarbon INFO @ 15:11:41] Tracking Intel CPU via RAPL interface\n",
      "[codecarbon INFO @ 15:11:43] >>> Tracker's metadata:\n",
      "[codecarbon INFO @ 15:11:43]   Platform system: Linux-5.15.0-75-generic-x86_64-with-glibc2.10\n",
      "[codecarbon INFO @ 15:11:43]   Python version: 3.8.12\n",
      "[codecarbon INFO @ 15:11:43]   CodeCarbon version: 2.2.4\n",
      "[codecarbon INFO @ 15:11:43]   Available RAM : 15.499 GB\n",
      "[codecarbon INFO @ 15:11:43]   CPU count: 4\n",
      "[codecarbon INFO @ 15:11:43]   CPU model: Intel(R) Core(TM) i7-7600U CPU @ 2.80GHz\n",
      "[codecarbon INFO @ 15:11:43]   GPU count: None\n",
      "[codecarbon INFO @ 15:11:43]   GPU model: None\n",
      "[codecarbon WARNING @ 15:11:46] Unable to access geographical location. Using 'Canada' as the default value - Exception : Expecting value: line 1 column 1 (char 0) - url=https://get.geojs.io/v1/ip/geo.json\n"
     ]
    }
   ],
   "source": [
    "tracker = EmissionsTracker()\n",
    "tracker.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[codecarbon INFO @ 15:11:46] Energy consumed for RAM : 0.000000 kWh. RAM Power : 5.812030792236328 W\n",
      "[codecarbon INFO @ 15:11:46] Energy consumed for all CPUs : 0.000000 kWh. Total CPU Power : 16.781044401284174 W\n",
      "[codecarbon INFO @ 15:11:46] 0.000000 kWh of electricity used since the beginning.\n",
      "[codecarbon INFO @ 15:11:46] Backing up old emission file\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.056024302806077e-07 gCO2eq\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    # Compute-intensive code goes here\n",
    "    for i in range(10,1):\n",
    "        print(i)\n",
    "finally:\n",
    "    emissions = tracker.stop()\n",
    "print(f\"{emissions * 1_000} gCO2eq\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": {
       "cloud_provider": "",
       "cloud_region": "",
       "codecarbon_version": "2.2.4",
       "country_iso_code": "CAN",
       "country_name": "Canada",
       "cpu_count": 4,
       "cpu_energy": 7.08683900261381e-8,
       "cpu_model": "Intel(R) Core(TM) i7-7600U CPU @ 2.80GHz",
       "cpu_power": 16.781044401284174,
       "duration": 0.0184323787689209,
       "emissions": 2.0560243028060772e-10,
       "emissions_rate": 1.11544165220431e-8,
       "energy_consumed": 8.648864957467916e-8,
       "gpu_count": null,
       "gpu_energy": 0,
       "gpu_model": null,
       "gpu_power": 0,
       "latitude": 46.8,
       "longitude": -71.2,
       "on_cloud": "N",
       "os": "Linux-5.15.0-75-generic-x86_64-with-glibc2.10",
       "project_name": "codecarbon",
       "pue": 1,
       "python_version": "3.8.12",
       "ram_energy": 1.562025954854107e-8,
       "ram_power": 5.812030792236328,
       "ram_total_size": 15.498748779296875,
       "region": "quebec",
       "run_id": "c38bbc73-e647-4068-b04c-5847aed48767",
       "timestamp": "2023-07-01T15:11:46",
       "tracking_mode": "machine"
      },
      "text/plain": [
       "<IPython.core.display.JSON object>"
      ]
     },
     "execution_count": 6,
     "metadata": {
      "application/json": {
       "expanded": false,
       "root": "root"
      }
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import JSON\n",
    "JSON(tracker.final_emissions_data.toJSON())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
